﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CRM.DataAccess.Entities;
using CRM.Business.Services;
using CRM.Encryption;
using Telerik.Web.UI;

namespace Web.SearchPopUp
{
    public partial class Organization : System.Web.UI.Page
    {
        static USER user;
        static long? assignedToID = null;
        static List<ORGANIZATION> orgs;
        static ROLE r;
        static string Flag = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;
            assignedToID = null;
            user = (USER)Session["User"];
            if (user != null)
            {
                this.RadGrid1.ClientSettings.Selecting.UseClientSelectColumnOnly = true;
                Flag = Request.QueryString["flag"];
                if (Flag != null && Flag.Trim() != "")
                {
                    if (Flag == "true")
                        this.RadGrid1.AllowMultiRowSelection = true;
                }
                LoadAllUser();
                DateTime dt = DateTime.Now;
                DateTime dt2 = dt.AddMonths(-3);
                dpDateCreatedFrom.SelectedDate = new DateTime(dt2.Year, dt2.Month, 1);
                dpDateCreatedTo.SelectedDate = DateTime.Now;
                r = RoleService.GetRoleByID((long)user.ROLE_ID);
                if ((!(bool)user.IS_ADMIN) && (!(bool)r.IS_MANAGER))
                {
                    divAssignTo.Visible = false;
                    assignedToID = user.ID;
                }
                orgs = getDataSource(dpDateCreatedFrom.SelectedDate, dpDateCreatedTo.SelectedDate.Value.AddHours(24), assignedToID, "", "", "");
                RadGrid1.DataSource = orgs;
                RadGrid1.DataBind();
            }
        }
        private void LoadAllUser()
        {
            ddlAssignedTo.Items.Clear();
            List<USER> users = UserService.ListAll();
            if (users != null)
            {
                foreach (USER u in users)
                {
                    ddlAssignedTo.Items.Add(new RadComboBoxItem(u.SUR_NAME + " " + u.NAME, u.ID.ToString()));
                }
            }
            ddlAssignedTo.Items.Insert(0, new RadComboBoxItem("Select an option", "-1"));
        }
        private List<ORGANIZATION> getDataSource(DateTime? dateFrom, DateTime? dateTo, long? assignedToID, string orgname, string conNumber, string primaryemail)
        {
            List<ORGANIZATION> dt = OrganizationService.GetCompanyByCondition(dateFrom, dateTo, assignedToID, orgname, conNumber, primaryemail);
            if (dt == null)
            {
                dt = new List<ORGANIZATION>();
            }
            return dt;
        }
        protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            RadGrid1.DataSource = orgs;
        }

        protected void btnSearch_ServerClick(object sender, EventArgs e)
        {
            if (divAssignTo.Visible)
            {
                long? at = Convert.ToInt64(ddlAssignedTo.SelectedValue);
                assignedToID = at == -1 ? null : at;
            }
            orgs = getDataSource(dpDateCreatedFrom.SelectedDate, dpDateCreatedTo.SelectedDate.Value.AddHours(24), assignedToID, txtCompanyName.Text.Trim(), txtContactNumber.Text.Trim(), txtPrimaryEmail.Text.Trim());
            RadGrid1.DataSource = orgs;
            RadGrid1.DataBind();
        }
        protected void RadAjaxManager1_AjaxRequest(object sender, Telerik.Web.UI.AjaxRequestEventArgs e)
        {
            if (e.Argument == "InitialPageLoad")
            {
                //simulate longer page load        
                System.Threading.Thread.Sleep(1000);
                Panel1.Style["display"] = "block";
            }
        }

        protected void RadGrid1_PreRender(object sender, EventArgs e)
        {
            RadGrid1.MasterTableView.GetColumn("ASSIGN_TO").Visible = true;
            if ((!(bool)user.IS_ADMIN) && (!(bool)r.IS_MANAGER))
            {
                RadGrid1.MasterTableView.GetColumn("ASSIGN_TO").Visible = false;
                RadGrid1.MasterTableView.GetColumn("ORGANIZATION_NAME").ItemStyle.Width = Unit.Percentage(25);
                RadGrid1.MasterTableView.GetColumn("ORGANIZATION_NAME").HeaderStyle.Width = Unit.Percentage(25);
                RadGrid1.MasterTableView.GetColumn("COMPANY_WEBSITE").ItemStyle.Width = Unit.Percentage(25);
                RadGrid1.MasterTableView.GetColumn("COMPANY_WEBSITE").HeaderStyle.Width = Unit.Percentage(25);
            }
            else
            {
                GridColumn col = RadGrid1.MasterTableView.GetColumn("ASSIGN_TO");
                if (col != null)
                {
                    foreach (GridDataItem dataItem in RadGrid1.Items)
                    {
                        RadComboBoxItem assignedToItem = ddlAssignedTo.FindItemByValue(dataItem[col].Text);
                        if (assignedToItem != null)
                            dataItem[col].Text = assignedToItem.Text;
                    }
                }
            }
        }

      
    }
}